//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    lists: [],
    dpxinxi: null,
    url: '',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    dztjcp: null,
    zpccp: null,
    hidden: true,
    pointfood: [],
    i: 0,
    u: 0,
    stype: 1,
    list: [],
    totalprice: 0,
    totalnum: 0,
    id: 0,
    stype: '',
    hiddennn: false,
    bannerimgs: [],
    beijing_url: '../images/beijing.jpg',
    titleimg: ' ',
    desk_open: '',
    url: '',
  },
  _loading: function () {
    var that = this
    wx.request({
      url: app.globalData._new_url + 'Product/GetProAndSetMealList',
      data: { shopId: app.globalData.shopids },
      method: 'post',
      success: function (res) {
        if (res.data.value == "未找到数据") {
          wx.showModal({
            title: '温馨提示',
            content: '暂无商品！',
            showCancel: false
          })
          return;
        } else {
          var _json_tj = JSON.parse(res.data.value)

          var list1 = new Array()
          var k = 0;
          for (var i = 0; i < _json_tj.length; i++) {
            if (_json_tj[i].IsHot == true) {
              if (k < 8) {
                list1.push(_json_tj[i])
                k++;
              }
            }
          }
          app.globalData.list = _json_tj
          app.globalData.slist = _json_tj
          that.setData({
            list: _json_tj,
            dztjcpa: _json_tj,
            dztj_list: list1,
            // zpc_list: list1
          })
        }
      }
    })
  },
  onLoad: function (options) {
    var that = this
    if (options.deskid != undefined) {
      var deskid = decodeURI(options.deskid)
      app.globalData.deskid = deskid
    }
    var cate_id = app.globalData.shopids
    wx.request({
      url: app.globalData._new_url + '/MenuAPI/Shop/GetEntity',
      data: { id: cate_id },
      method: 'post',
      success: function (res) {
        // console.log('sssss', res)
        app.globalData.shop_infos = res.data.value
        var array = new Array()
        if (res.data.value.Banner1) { array.push(res.data.value.Banner1.substring(1)) }
        if (res.data.value.Banner2) { array.push(res.data.value.Banner2.substring(1)) }
        if (res.data.value.Banner3) { array.push(res.data.value.Banner3.substring(1)) }
        that.setData({ Address: app.globalData.shop_infos.Address, bannerimgs: array, titleimg: app.globalData.shop_infos.LOGO, beijing_url: app.globalData.shop_infos.ShopBackgroundImage, wifi: app.globalData.shop_infos.WIFIAccount, wifimm: app.globalData.shop_infos.WIFIPassword })
        wx.setNavigationBarTitle({
          title: app.globalData.shop_infos.FullName,
        })
      }
    })
    // 店长推荐
    that._loading()
    wx.getLocation({
      success: function (res) { wx.setStorageSync('location', res) },
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight + 430,
          xqHeight: res.windowWidth * 0.9,
        })
      }
    })

    that.setData({
      desk_open: app.globalData.desk_open,
      url: app.globalData._new_url,
    })
    // cs登陆
    wx.login({
      success: function (res) {
        wx.setStorageSync("code", res.code)
        // wx.getUserInfo({
        //   success: function (res) {
        //     app.globalData.userInfo = res.userInfo 
        //   }
        // })
        if (res.code != '') {
          //发起网络请求
          var code = wx.getStorageSync("code")
          console.log("appid:", app.globalData.appid, "app密钥", app.globalData.secret, "开关", app.globalData.isDiancanFlow, 'shopId', app.globalData.shopids, "code:", code)
          wx.request({
            url: app.globalData._new_url + 'WeChatLogin/GetSessionFormal',
            data: {
              code: code,
              nickName: "",
              appid: app.globalData.appid,
              secret: app.globalData.secret,
              isDiancanFlow: app.globalData.isDiancanFlow,
              shopId: app.globalData.shopids
            },
            method: 'post',
            success: function (res) {
              console.log(res)
              app.globalData.openId = res.data.openid
            }, fail: function (err) {
              console.log(err)
            }
          })
        } else {
          wx.showModal({
            title: '温馨提示',
            content: '获取用户登录态失败！',
            showCancel: false
          })
        }
      }
    })


  },
  navtoJieshao: function () {
    wx.navigateTo({
      url: '../jieshao/index',
    })
  },
  navtoRemen: function (e) {

    wx.navigateTo({
      url: '/pages/foods/remen?type=' + e.currentTarget.dataset.type
    })
  }, navtoRemen2: function (e) {
    wx.switchTab({
      url: '/pages/order/index',
    })
  },
  navtowifi: function () {

    this.setData({
      hiddennn: true
    })
  },
  shouqilaia: function () {
    this.setData({
      hiddennn: false
    })
  },
  openMap: function () {
    var that = this
    var latitude = parseFloat(app.globalData.shop_infos.Weidu)
    var longitude = parseFloat(app.globalData.shop_infos.Jingdu)
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      name: app.globalData.shop_infos.FullName,
      address: app.globalData.shop_infos.Address,
      success: function (e) {
        console.log(e)
      },
      complete: function (e) {
        console.log(e)
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  navtoyuyue: function (e) {
    wx.navigateTo({
      url: '/pages/yuyue/index'
    })
  },
  navtopaidui: function () {
    // wx.navigateTo({
    //   url: '/pages/paidui/index',
    // })
    wx.switchTab({
      url: '../order/index',
    })
  },
  call: function () {
    wx.makePhoneCall({
      phoneNumber: app.globalData.shop_infos.Telephone  //仅为示例，并非真实的电话号码
    })
  },
  tanchu: function (e) {
    var productId = e.currentTarget.dataset.sid

    var id = e.currentTarget.dataset.id
    var ssimg = e.currentTarget.dataset.ssimg
    var ssname = e.currentTarget.dataset.ssname
    var ssdescription = e.currentTarget.dataset.description
    var ssprice = e.currentTarget.dataset.ssprice
    this.setData({
      hidden: '',
      ssimg: ssimg,
      ssname: ssname,
      lista: this.data.dztjcpa[id],
      ssprice: ssprice,
      ssdescription: ssdescription,
      chooseCurrentProductId: productId,
    })
    for (var i = 0; i < this.data.list.length; i++) {
      var current = this.data.list[i];
      if (this.data.chooseCurrentProductId == current.Id) {
        this.data.chooseCurrentProductNumber = current.number
        this.setData({
          chooseCurrentProductNumber: this.data.chooseCurrentProductNumber
        })
        break
      }
    }
  },
  shouqi: function () {
    this.setData({
      hidden: true
    })
  },
  addfoods2: function (e) {
    for (var i = 0; i < this.data.list.length; i++) {
      var current = this.data.list[i];
      if (this.data.chooseCurrentProductId == current.Id) {
        this.data.chooseCurrentProductNumber = current.number
        this.setData({
          chooseCurrentProductNumber: this.data.chooseCurrentProductNumber
        })
        break
      }
    }

    if (this.data.chooseCurrentProductId != '') {
      if (this.data.list.length > 0) {
        for (var i = 0; i < this.data.list.length; i++) {
          var current = this.data.list[i];
          if (this.data.chooseCurrentProductId == current.Id) {
            this.data.list[i].number++
            this.data.chooseCurrentProductNumber = this.data.list[i].number;
            this.setData({
              list: this.data.list,
              chooseCurrentProductNumber: this.data.chooseCurrentProductNumber
            })

            break;
          }
        }

      }

      var totalprice = 0
      var totalnum = 0
      totalnum = this.data.totalnum + 1
      totalprice = parseFloat(this.data.totalprice) + parseFloat(e.currentTarget.dataset.price)
      console.log(totalprice)
      totalprice = app.returnFloat(totalprice)
      this.setData({
        totalprice: totalprice,
        totalnum: totalnum
      })
      app.globalData.totalprice = this.data.totalprice
      app.globalData.list = this.data.list

    }


  },
  jianfoods2: function (e) {
    for (var i = 0; i < this.data.list.length; i++) {
      var current = this.data.list[i];
      if (this.data.chooseCurrentProductId == current.Id) {
        this.data.chooseCurrentProductNumber = current.number
        this.setData({
          chooseCurrentProductNumber: this.data.chooseCurrentProductNumber
        })
        break
      }
    }

    if (this.data.chooseCurrentProductId != '') {
      if (this.data.list.length > 0) {
        for (var i = 0; i < this.data.list.length; i++) {
          var current = this.data.list[i];
          if (this.data.chooseCurrentProductId == current.Id) {
            this.data.list[i].number--
            this.data.chooseCurrentProductNumber = this.data.list[i].number;
            this.setData({
              list: this.data.list,
              chooseCurrentProductNumber: this.data.chooseCurrentProductNumber
            })

            break;
          }
        }

      }

      var totalprice = 0
      var totalnum = 0
      totalnum = this.data.totalnum - 1
      totalprice = parseFloat(this.data.totalprice) - parseFloat(e.currentTarget.dataset.price)
      console.log(totalprice)
      totalprice = app.returnFloat(totalprice)
      this.setData({
        totalprice: totalprice,
        totalnum: totalnum
      })
      app.globalData.totalprice = this.data.totalprice
      app.globalData.list = this.data.list

    }
  },
  onShow: function () {
    if (this.data.list != '') {
      this.setData({
        list: app.globalData.list,
        totalprice: app.globalData.totalprice
      })
    }

  },
  onHide: function () {
    this.setData({
      hidden: true
    })
  },
  onShareAppMessage: function () {

  },
  toloadpage: function (e) {
    wx.redirectTo({
      url: '../load/load',
    })
  }
})
